package xkp.lesson.dao;

import xkp.lesson.entity.UserInfo;
import xkp.lesson.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserInfoDao implements BaseDao<UserInfo> {

    @Override
    public List<UserInfo> selectAll() {
        return null;
    }

    @Override
    public UserInfo selectById(int id) throws SQLException, ClassNotFoundException {
        UserInfo userInfo = null;
        //1、打开连接
        Connection connection = DBUtils.openConn();
        //2、sql语句
        String sql = "select * from user_info where user_id = ?";
        //3、预处理对象
        PreparedStatement statement = connection.prepareStatement(sql);
        //4、填充参数
        statement.setInt(1,id);
        //5、获取结果集
        ResultSet set = statement.executeQuery();
        //6、遍历结果集
        while(set.next()){
            userInfo = new UserInfo();
            String userName = set.getString("user_name");
            String userPwd = set.getString("user_pwd");
            Integer userAge = set.getInt("user_age");
            userInfo.setUserId(id);
            userInfo.setUserName(userName);
            userInfo.setUserAge(userAge);
            userInfo.setUserPwd(userPwd);
        }
        set.close();
        statement.close();
        connection.close();
        return userInfo;
    }

    @Override
    public int insert(UserInfo userInfo) {
        return 0;
    }

    @Override
    public int deleteById(int id) {
        return 0;
    }

    @Override
    public int update(UserInfo userInfo) {
        return 0;
    }
}
